﻿using System.Collections.Generic;
using System.Configuration;
using System.Web.Mvc;
using DevTrends.MvcDonutCaching;
using ECG.Common;
using ECG.DAL;
using ECG.WebUI.CustomHelpers;
using ECG.WebUI.Models;

namespace ECG.WebUI.Controllers
{
    public class SearchController : Controller
    {
        //
        // GET: /Search/
        public ActionResult RidirectSearch(string keyword = "")
        {
            if (string.IsNullOrEmpty(keyword))
            {
                return RedirectToAction("Index", "Home");
            }
            return RedirectToAction("Index", "Search", new { keyword = keyword.ReplaceSpecialAsEmpty() });
        }

        public ActionResult Index(string keyword = "", int page = 1)
        {
            ViewBag.Keyword = keyword;
            ViewBag.Page = page;
            return View();
        }

        [DonutOutputCache(CacheProfile = "OneDay")]
        public ActionResult ProductByKeyword(string keyword = "", int page = 1)
        {
            var itemsPerPage = int.Parse(ConfigurationManager.AppSettings["ProductListByCateIdPageSize"]);
            var totalPage = 0;
            var model = ProductDb.GetAllProductByKeyword(page, itemsPerPage, ref totalPage, FunctionHelpers.RemoveSpecialCharsV2(keyword)) ?? new List<ProductRateByCateTopListPagingDto>();
            ViewBag.Keyword = keyword;
            ViewBag.PageInfo = new PageInfo { CurrentPage = page, PageSize = itemsPerPage, TotalPages = totalPage };
            return PartialView(model);
        }

    }
}
